/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unicamp.ic.sgct.client.apresentacao.views;

import br.unicamp.ic.sgct.client.apresentacao.widgets.LabelCampoForm;
import br.unicamp.ic.sgct.client.dominio.to.AvaliacaoTO;
import br.unicamp.ic.sgct.client.dominio.to.StatusAvaliacao;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import java.util.List;
import org.gwtiger.client.widget.HoverGridWidget;

/**
 *
 * @author arthur
 */
public class ViewListaAvaliacao extends Composite {

    private HoverGridWidget gridListaAvaliacao = new HoverGridWidget();
    private VerticalPanel panelListaAvaliacao = new VerticalPanel();
    private HorizontalPanel panelHorizontalADD = new HorizontalPanel();
    private List<AvaliacaoTO> avaliacoes;
    private HandlerClickListaAvaliacao handler;
    private HTML strTabTitle = null;
    private int index = 0;

    public ViewListaAvaliacao(String title, int index, HandlerClickListaAvaliacao handler) {
        this.index = index;
        this.strTabTitle = new HTML(title);

        this.handler = handler;
        initPanel();

        initWidget(panelListaAvaliacao);
    }

    public ViewListaAvaliacao addH(Widget w) {
        panelHorizontalADD.add(w);
        return this;
    }

    public void clearH() {
        panelHorizontalADD.clear();
    }

    public ViewListaAvaliacao addV(Widget w) {
        panelListaAvaliacao.add(w);
        return this;
    }

    /**
     * @return the avaliacoes
     */
    public List<AvaliacaoTO> getAvaliacoes() {
        return avaliacoes;
    }

    public interface HandlerClickListaAvaliacao {

        public void handle(AvaliacaoTO to);
    }

    private void initPanel() {
        panelListaAvaliacao.setVisible(false);
        panelListaAvaliacao.add(new LabelCampoForm("Avaliaç\u00f5es"));
        initGrid();
        panelListaAvaliacao.setCellWidth(gridListaAvaliacao, "70%");
        panelListaAvaliacao.add(gridListaAvaliacao);
        panelListaAvaliacao.add(panelHorizontalADD);
        //panelListaUsuario.add(panelDadosParticipacaoUsuario);

    }

    private void initGrid() {
        gridListaAvaliacao.setHoverStyleName("grid-hover");
        gridListaAvaliacao.setWidth(500 + "px");
        gridListaAvaliacao.addClickHandler(new ClickHandler() {

            private int index;
            private boolean visible;

            @Override
            public void onClick(ClickEvent event) {
                Cell cell = gridListaAvaliacao.getCellForEvent(event);
                int i = cell.getRowIndex() - 1;
                if (this.index == i && visible) {
                    visible = false;
                } else {
                    visible = true;
                    AvaliacaoTO to = getAvaliacoes().get(i);
                    //montarDadosUniario(to);
                    handler.handle(to);
                    //outer.showLoading(true);
                }
                this.index = i;
            }
        });
    }
    String tet = null;

    public void montar(final List<AvaliacaoTO> avaliacoes) {
        this.avaliacoes = avaliacoes;
        gridListaAvaliacao.clearGrid();
        if (avaliacoes.isEmpty()) {
            panelListaAvaliacao.setVisible(false);
        } else {
            panelListaAvaliacao.setVisible(true);
        }
        if (avaliacoes != null && !avaliacoes.isEmpty()) {
            gridListaAvaliacao.addHeader(1, "  Status  ");
            gridListaAvaliacao.addHeader(2, "  Data  ");
            gridListaAvaliacao.addHeader(3, "  Avaliador  ");
            gridListaAvaliacao.addHeader(4, "  Revis\u00e3o  ");
            gridListaAvaliacao.addHeader(5, "  Submiss\u00e3o  ");
            RowFormatter rowFormatter = gridListaAvaliacao.getRowFormatter();
            AvaliacaoTO avaliacao;
            for (int row = 0; row < avaliacoes.size(); row++) {
                avaliacao = avaliacoes.get(row);
                if (row + 1 % 2 == 0) {
                    rowFormatter.setStyleName(row + 1, HoverGridWidget.STYLE_ROW_EVEN);
                } else {
                    rowFormatter.setStyleName(row + 1, HoverGridWidget.STYLE_ROW_ODD);
                }
                gridListaAvaliacao.setId(row + 1, String.valueOf(avaliacao.getStatus().getValue()));
                String color = "#000000";
                if(avaliacao.getStatus() ==  StatusAvaliacao.Aceito){
                    color = "#000088";
                }else{
                    if(avaliacao.getStatus() ==  StatusAvaliacao.Recusado){
                        color = "#880000";
                        
                    }else{
                        if(avaliacao.getStatus() ==  StatusAvaliacao.Pendente){
                            color = "#008800";
                        }
                    }
                }
                gridListaAvaliacao.setHTML(row + 1, 1, "<b><FONT COLOR=\""+color+"\">" + avaliacao.getStatus().getValue() + "</FONT></b>");
                gridListaAvaliacao.setText(row + 1, 2, ViewUtil._dtTimeFormat.format(avaliacao.getDataAvaliacao()));
                gridListaAvaliacao.setText(row + 1, 3, avaliacao.getAvaliador().getUsername());
                if(avaliacao.getRevisao() == null){
                gridListaAvaliacao.setText(row + 1, 4, "");
                gridListaAvaliacao.setText(row + 1, 5, "");
                }else{
                gridListaAvaliacao.setText(row + 1, 4, ViewUtil._dtTimeFormat.format(avaliacao.getRevisao().getData()));
                gridListaAvaliacao.setText(row + 1, 5, avaliacao.getRevisao().getSubmissao() == null ? "" : ViewUtil._dtTimeFormat.format(avaliacao.getRevisao().getSubmissao().getDataSubmissao()));
                }
            }
        }

    }
    
    public HTML getStrTabTitle() {
        return strTabTitle;
    }
}
